

package Game;

import Drawing.TankImage;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * This class holds all variables about a match between players (tanks)
 * Compiler: jdk1.6.0_14 <br />
 * IDE: NetBeans 6.7.1 <br />
 * @author Andrew Smith <br />
 */
public class Match
{

    public static final String VERSION_NO = "Build 2 (21/10/2009)";

    public static final int BOUNDS_WIDTH = 700;
    public static final int BOUNDS_HEIGHT = 650;
    
    public static final int START_BOX = 80;
    public static final Rectangle PLAYER_1_START_BOX = new Rectangle(0,0,START_BOX,START_BOX);
    public static final Rectangle PLAYER_2_START_BOX = new Rectangle(BOUNDS_WIDTH-START_BOX, BOUNDS_HEIGHT-START_BOX, START_BOX, START_BOX);

    /** the map that this match will be played on */
    private Map map;
    /** a list of all the players in this match */
    private ArrayList<Tank> tanks;
    /** the listener to listen for all things happening in the match */
    private Listener listener;
    /** a list of all the listeners listening to this match */
    private ArrayList<ActionListener> listeners;

    /**
     * Sets up a default match with no players
     */
    public Match()
    {
        setup();
        System.out.println("Exits setup");
    }

    /**
     * Sets up a match with two players
     * @param player1 the first player
     * @param player2 the second player
     */
    public Match(Tank player1, Tank player2)
    {
        setup();
        addTank(player1);
        addTank(player2);
    }

    /**
     * sets up the match by init all objects
     */
    private void setup()
    {
        map = new Map();
        System.out.println("Map Created");
        tanks = new ArrayList<Tank>();
        listener = new Listener();
        listeners = new ArrayList<ActionListener>();
    }

    /**
     * Adds a tank to this match
     * @param player the player's tank to add
     */
    public void addTank(Tank player)
    {
        player.addActionListener(listener);
        tanks.add(player);
    }


    /**
     * Paints everything about this match 
     * @param g the graphics to paint on
     */
    public void paint(Graphics g)
    {
        map.paint(g);
        for(int i=0; i<tanks.size(); i++)
        {
            TankImage img = new TankImage(tanks.get(i));
            img.draw(g);
        }
    }

    /**
     * Checks to see if the positions specified is a valid position
     * (ie no rocks in the way)
     * @param xPoints
     * @param yPoints
     * @return true if the points specified are not in the way of any rocks, false otherwise.
     */
    public boolean safeMove(int[] xPoints, int[] yPoints)
    {
        for(int i=0; i<xPoints.length; i++)
        {
            if(!map.validPoint(xPoints[i], yPoints[i])) //if this returns false
            {   return false;
            }
        }
        return true;
    }

    /**
     * Adds a new listener onto this to check for updates
     * @param listener the listener to watch this shot
     */
    public void addActionListener(ActionListener listener)
    {
        listeners.add(listener);
    }


    /** an update id for actionevents */
    private int update_id = 0;
    //valid messages for the notifyListeners method
    //TODO add messages
    public static final String REPAINT_MESSAGE = "repaint";

    /**
     * Notifies all the listeners with this
     */
    private void notifyListeners(String message)
    {
        for(int i=0; i<listeners.size(); i++)
        {
            update_id++;
            ActionEvent e = new ActionEvent(this, update_id, message);
            if(listeners.get(i)!=null)
            {   listeners.get(i).actionPerformed(e);
            }
        }
    }


    
    /**
     * Inner class to listen for all updates
     */
    private class Listener implements ActionListener
    {

        public void actionPerformed(ActionEvent e)
        {
            String message = e.getActionCommand();
            System.out.println(message);
            //at the moment if anything is done it will send out an event saying repaint
            notifyListeners(REPAINT_MESSAGE);
        }

    }

}
